<template>
  <div v-if="!showSearchNote" class="ppt" @touchend="active = false">
    <div class="header">
      <span>报告主题</span>
      <span> (必填项)</span>
      <van-cell-group class="input">
        <van-field style="background-color: #f0f0f0;" v-model="formData.title" placeholder="请输入报告主题" />
      </van-cell-group>
    </div>

    <div class="content">
      <div class="search">
        <SvgIcon name="笔记3" size="20" style="color:#fff;" />
        <span>添加笔记</span>
        <span> (必填项)</span>
      </div>
      <div class="time">
        <span>时间</span>
        <van-cell-group style="display:flex;">
          <van-field readonly clickable name="calendar" v-model="formData.startDate" :value="formData.startDate"
            placeholder="开始时间" @click="showCalendarStart = true" />
          <van-field readonly clickable clearable name="calendar" v-model="formData.endDate" :value="formData.endDate"
            placeholder="结束时间" @click="showCalendarEnd = true">
            <template #right-icon>

              <div class="search-icon">
                <SvgIcon v-show="formData.startDate.length !== 0 || formData.endDate.length !== 0"
                  @click.stop="clearDate" class="icon-item" name="清空" :size="24" />
              </div>

            </template>
          </van-field>
        </van-cell-group>
        <van-calendar v-model:show="showCalendarStart" @confirm="onConfirmStart" :min-date="startDate"/>
        <van-calendar v-model:show="showCalendarEnd" @confirm="onConfirmEnd" :min-date="endDate"/>
      </div>
      <div class="addr">
        <span>地点</span>
        <van-cell-group class="input">
          <van-field style="background-color: #f0f0f0;" v-model="formData.addr" placeholder="例如：北京市" />
        </van-cell-group>
      </div>
      <div class="searchAI">
        <span>AI搜索关键信息</span>
        <van-cell-group class="input">
          <van-field style="background-color: #f0f0f0;" v-model="formData.search" placeholder="上周的工作笔记">
            <template #right-icon>
              <div class="search-icon">
                <SvgIcon v-show="formData.search.length !== 0" @click="clearContent" class="icon-item" name="清空"
                  :size="24" />
                <van-button @click="querySubmit" style="width: 76px;height: 36px;white-space: nowrap;" type="primary">
                  AI搜索
                </van-button>
              </div>
            </template>

          </van-field>
        </van-cell-group>
      </div>
      <van-divider :style="{ color: '#1989fa', borderColor: '#1989fa' }" />
      <div class="note">
        <span>已添加笔记</span>
        <van-button style="width:100px;height: 30px;white-space: nowrap;" @click="clearContent" round icon="plus" plain
          type="primary">添加</van-button>
      </div>
      <div class="cardList">
        <div class="cardItem" v-for="(item,index) in noteList">
          <div class="cardHeader">
            <span>{{ moment(item.create_time).format('MM/DD HH:mm') }}</span>
            <span>{{item.address}}</span>
          </div>
          <div class="text">
            笔记标题—{{item.file_name}}
          </div>
          <SvgIcon class="icon-item" @click = "clearNote(index)" name="清除" :size="20" style="opacity: .2;" />
        </div>
      </div>
    </div>
    <div class="footer">
      <van-button type="primary" style="width: 100%;" @click = "generationPPT"> 生成</van-button>

    </div>
  </div>
  <searchNote v-else @back="showSearchNote = false" @selectedData="selected"></searchNote>
</template>

<script setup>
import SvgIcon from "@/components/SvgIcon.vue";
import { showNotify } from 'vant';
import moment from "moment";
import searchNote from "@/views/generation/openSearch.vue"

import { ref } from "vue"
import { useRouter } from "vue-router";
import {filtration, listNote} from "@/api/index.js";

const showSearchNote = ref(false);
const router = useRouter()
const message = ref('')
const active = ref(false)
const show = ref(false)
const startDate = ref(new Date(2000, 1, 1))
const nowDate = ref(new Date())
const endDate = ref(new Date(2000, 1, 1))
const dataList = ref([])
const formData = ref(
  {
    title: '',
    startDate: '',
    endDate: '',
    addr: '',
    search: '',
    filePaths: []
  }
)
const noteList = ref([])
const showCalendarStart = ref(false)
const showCalendarEnd = ref(false)

const querySubmit = async() =>{
  const data = await filtration({start_time_str:formData.value.startDate,end_time_str:formData.value.endDate,address:formData.value.addr,content:formData.value.search})
  noteList.value = data.data
}

const generationPPT = () =>{
  if(formData.value.title === ''){
    showNotify({
      message: '请输入报告主题',
      color: '#fff',
      background: '#ff0000'
    })
    return
  }
  noteList.value.forEach(item => {
    formData.value.filePaths.push("/DATA/notes/" + item.file_name)
  })

  router.push({ path: '/generationPPT', query: { message: formData.value.title, filePaths:formData.value.filePaths.toString()  } })
}

const clearContent = () => {
  formData.value.search = ''
  showSearchNote.value = true
}

const selected = (data) =>{
  showSearchNote.value = false
  noteList.value = data
  console.log(noteList.value)
}
const clearNote = (index) =>{
  noteList.value.splice(index,1)
}

const onConfirmStart = (date) => {
  endDate.value = new Date(date.getFullYear(),date.getMonth(),date.getDate());
  formData.value.startDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 00:00:00`;
  showCalendarStart.value = false;
}
const onConfirmEnd = (date) => {
  formData.value.endDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 23:59:59`;
  showCalendarEnd.value = false;
}
//清空时间
const clearDate = () => {
  formData.value.startDate = ''
  formData.value.endDate = ''

}
</script>

<style lang="scss" scoped>
.ppt {
  padding: 12px;

  .header {
    height: 110px;
    border-radius: 7px;
    background: #FFF;
    padding: 10px 20px;
    //height: 74px;
    //margin: 0 12px 32px;

    span {
      &:nth-child(1) {
        color: #191919;
        font-size: 15px;
        font-weight: 700;
      }

      &:nth-child(2) {
        color: #999;
        font-size: 14px;
      }
    }

    .input {
      margin-top: 12px;
    }
  }

  .content {
    margin-top: 15px;
    //display: flex;
    //justify-content: center;
    margin-bottom: 10px;
    height: 800px;
    border-radius: 7px;
    background: #FFF;
    padding: 10px 20px;

    .search {
      display: flex;
      margin-bottom: 10px;

      span {
        &:nth-child(2) {
          color: #191919;
          font-size: 15px;
          font-weight: 700;
        }

        &:nth-child(3) {
          color: #999;
          font-size: 14px;
        }
      }
    }

    .time {

      span {
        font-size: 14px;
        font-weight: 400;
      }

      :deep(.van-cell--clickable) {
        background-color: rgb(240, 240, 240);
      }
    }

    .addr {
      margin-top: 10px;

      span {
        font-size: 14px;
        font-weight: 400;
      }

      .input {
        margin-top: 15px;
      }
    }

    .searchAI {
      margin-top: 10px;

      span {
        font-size: 14px;
        font-weight: 400;
      }

      .input {
        margin-top: 15px;

        .search-icon {
          display: flex;
          align-items: center;
          justify-content: center;

          .icon-item {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;

            color: #fff;

            &:nth-child(2) {
              border-radius: 50%;
              padding: 4px;
              color: #000000;
              transition: 1s;
            }
          }

          .active {
            background-color: var(--van-primary-color);
            color: #fff !important;
          }
        }
      }
    }

    .note {
      margin-top: 8px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .icon-note {
        :deep(.van-button__content) {
          .van-button__text {
            display: flex;
            align-items: center;

            span {
              &:nth-child(2) {
                width: 50px;
              }
            }
          }
        }
      }

      span {
        font-size: 14px;
        font-weight: 400;
        width: 100%;
        display: flex;
        justify-content: start;



      }
    }

    .cardList {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      height: 400px;
      overflow: hidden;
      overflow-y: auto;
      align-content: flex-start;

      .cardItem {
        width: 48%;
        background-color: #F2F4F9;
        margin-top: 10px;
        height: 90px;
        border-radius: 6px;
        padding-top: 15px;
        position: relative;

        .icon-item {
          position: absolute;
          right: 0px;
          top: 0px;
        }

        .cardHeader {
          color: #666;
          display: flex;
          width: 100%;
          padding: 0 10px;
          padding-right: 20px;
          justify-content: space-between;
          font-family: "PingFang SC";
          font-size: 12px;

        }

        .text {
          font-size: 14px;
          overflow: hidden;
          margin-top: 12px;
          width: 100%;
          padding: 0 10px;
          text-overflow: ellipsis;
          -webkit-line-clamp: 2;
          display: -webkit-box;
          -webkit-box-orient: vertical;
        }
      }
    }

  }

  .footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: #fff;
    height: 80px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 30px;
  }
}
</style>
